﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Summary description for Agent
/// </summary>
/// 
namespace LinqObj
{
    public partial class Agent
    {

        public static IEnumerable<Agent> GetAgentListByAgentGroupID(string agentGroupID)
        {
            return Agent.GetAgentListByAgentGroupID(Convert.ToInt32(agentGroupID));
        }

        public static IEnumerable<Agent> GetAgentListByAgentGroupID(int agentGroupID)
        {
            ToolboxDataContext context = new ToolboxDataContext();

            IEnumerable<Agent> agents = from _agent in context.GetTable<Agent>()
                                        where _agent.agent_group_id == agentGroupID
                                        select _agent;
            return agents.ToList<Agent>();
        }

        public static Agent GetAgentByAgentID(int agentID)
        {
            ToolboxDataContext context = new ToolboxDataContext();
            IEnumerable<Agent> agents = from _agent in context.GetTable<Agent>()
                                        where _agent.agent_id == agentID
                                        select _agent;
            return agents.SingleOrDefault();
        }

        public Agent(int _key, string _val)
        {
            this.agent_id = _key;
            this.agent_name = _val;
        }
    }
}
    //    private int agentID;
    //    private int agentGroupID;
    //    private string agentRoleName;
    //    private int agentRoleID;
    //    private string agentName;
    //    private string contactNumber;
    //    private string emailAddress;
    //    private int userID;

    //    public string AgentRoleName { get { return this.agentRoleName; } set { this.agentRoleName = value; } }
    //    public int AgentID { get { return this.agentID; } set { this.agentID = value; } }
    //    public int AgentGroupID { get { return this.agentGroupID; } set { this.agentGroupID = value; } }
    //    public int AgentRoleID { get { return this.agentRoleID; } set { this.agentRoleID = value; } }
    //    public string AgentName { get { return this.agentName; } set { this.agentName = value; } }
    //    public string EmailAddress { get { return this.emailAddress; } set { this.emailAddress = value; } }
    //    public string ContactNumber { get { return this.contactNumber; } set { this.contactNumber = value; } }
    //    public int UserID { get { return this.userID; } set { this.userID = value; } }

    //    public Agent() { }

    //    public Agent(int index, string selectHint)
    //    {
    //        this.agentName = selectHint;
    //        this.agentID = index;
    //    }

    //    public static IEnumerable<Agent> getAgentListByAgentGroupID(IDbConnection conn, String agentGroupID)
    //    {
    //        return getAgentListByAgentGroupID(conn, Convert.ToInt32(agentGroupID));
    //    }

    //    public static IEnumerable<Agent> getAgentListByAgentGroupID(IDbConnection conn, int agentGroupID)
    //    {
    //        string sql_script = @"select agent_id, agent_name, contact_number,
    //                        user_id, agent_group_id, email_address
    //                        from agent
    //                        where agent_group_id = " + agentGroupID;
    //        IDbCommand cmd = conn.CreateCommand();
    //        cmd.CommandText = sql_script;
    //        IDataReader dr = cmd.ExecuteReader();

    //        List<Agent> agentRoleList = new List<Agent>();

    //        while (dr.Read())
    //        {
    //            Agent tmp = new Agent();
    //            tmp.AgentID = dr.GetInt32(0);
    //            tmp.AgentName = dr.GetString(1);
    //            if (!dr.IsDBNull(2))
    //                tmp.ContactNumber = dr.GetString(2);
    //            if (!dr.IsDBNull(3))
    //                tmp.UserID = dr.GetInt32(3);
    //            if (!dr.IsDBNull(4))
    //                tmp.AgentGroupID = dr.GetInt32(4);
    //            if (!dr.IsDBNull(5))
    //                tmp.EmailAddress = dr.GetString(5);

    //            agentRoleList.Add(tmp);

    //        }
    //        return agentRoleList;
    //    }

    //    public static Agent getAgentByUserID(IDbConnection conn, int userID)
    //    {
    //        string sql_script =
    //                             @"select * from Agent where user_id = @user_id";
    //        IDbCommand cmd = conn.CreateCommand();
    //        cmd.CommandText = sql_script;
    //        IDbDataParameter param = cmd.CreateParameter();
    //        param.ParameterName = "@user_id";
    //        param.DbType = DbType.Int32;
    //        param.Value = userID;
    //        cmd.Parameters.Add(param);
    //        IDataReader dr = cmd.ExecuteReader();
    //        Agent tmp = new Agent();
    //        if (dr.Read())
    //        {
    //            tmp.agentID = Convert.ToInt32(dr["agent_id"].ToString());
    //            tmp.agentGroupID = Convert.ToInt32(dr["agent_group_id"].ToString());
    //        }
    //        return tmp;
    //    }

    //    public static Agent getAgentByUserID(IDbConnection conn, string strUserID)
    //    {
    //        int userID = Int32.Parse(strUserID);
    //        return getAgentByUserID(conn, userID);
    //    }

    //    public static string matchAgentByID(List<Agent> agentList, int agentID)
    //    {
    //        foreach (Agent agent in agentList)
    //        {
    //            if (agent.AgentID == agentID)
    //                return agent.AgentName;
    //        }
    //        return null;
    //    }

